//
//  XJLGroupListViewController.m
//  XJL_WeChat
//
//  Created by XJL on 16/8/23.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLGroupListViewController.h"
#import "XJLCreateGroupViewController.h"
#import "XJLChatController.h"

@interface XJLGroupListViewController ()

/** 组数组*/
@property(nonatomic,strong) NSArray *groupList;
@end

@implementation XJLGroupListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"群列表";
    //1.本地获取群信息
    self.groupList = [[EaseMob sharedInstance].chatManager groupList];
    // 2.本地没有群数据
    if (self.groupList.count == 0) {
        [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsListWithCompletion:^(NSArray *groups, EMError *error) {
            //获取服务器的群数据
            self.groupList = groups;
            [self.tableView reloadData];
        } onQueue:nil];
        
    }
    
    //添加创建群组的按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"创建群" style:0 target:self action:@selector(creaseGroupVc)];
}

-(void)creaseGroupVc{
    XJLCreateGroupViewController *cgVc = [[XJLCreateGroupViewController alloc]init];
    cgVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:cgVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.groupList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"GroupCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:0 reuseIdentifier:ID];
    }
    //获取组的数据模型
    EMGroup *group = self.groupList[indexPath.row];
    
    cell.textLabel.text = group.groupSubject;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EMGroup *group = self.groupList[indexPath.row];
    
    XJLChatController *chatVc = [[XJLChatController alloc]initWithConversationChatter:group.groupId conversationType:eConversationTypeGroupChat];
    chatVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chatVc animated:YES];
    
}



@end
